package com.jp.knowledge.my.view;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jp.knowledge.R;

/**
 * Created by 528787067@qq.com on 2017/6/13.
 */

public class IntegralTabView extends FrameLayout implements View.OnClickListener {
    public IntegralTabView(Context context) {
        super(context);
        init(context);
    }

    public IntegralTabView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public IntegralTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private View contentView;
    private TextView tabLeft;
    private TextView tabCenter;
    private TextView tabRight;

    private void init(Context context){
        contentView = View.inflate(context, R.layout.integral_tab_view, this);
        tabLeft = (TextView) contentView.findViewById(R.id.integral_tab_left);
        tabCenter = (TextView) contentView.findViewById(R.id.integral_tab_center);
        tabRight = (TextView) contentView.findViewById(R.id.integral_tab_right);
        tabLeft.setTag(0);
        tabCenter.setTag(1);
        tabRight.setTag(1);
        contentView.setTag(tabLeft);
        tabLeft.setOnClickListener(this);
        tabCenter.setOnClickListener(this);
        tabRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == contentView.getTag())
            return;
        ((View)contentView.getTag()).setBackground(null);
        ((TextView)contentView.getTag()).setTextColor(getResources().getColor(R.color.theme_color));
        ((TextView)v).setTextColor(getResources().getColor(R.color.white));
        contentView.setTag(v);
        if(this.onTabSelectedListener != null)
            this.onTabSelectedListener.onTabSelected((int)v.getTag());
        switch (v.getId()){
            case R.id.integral_tab_left:
                tabLeft.setBackground(getResources().getDrawable(R.drawable.integral_tab_view_left));
                break;
            case R.id.integral_tab_center:
                tabCenter.setBackgroundColor(getResources().getColor(R.color.theme_color));
                break;
            case R.id.integral_tab_right:
                tabRight.setBackground(getResources().getDrawable(R.drawable.integral_tab_view_right));
                break;
        }
    }

    public boolean setSelectedTab(int index){
        if(index > 1 || index < 0)
            return false;
        onClick(index == 0 ? tabLeft : /*index == 1 ? tabCenter :*/ tabRight);
        return true;
    }

    public int getSelectedTabIndex(){
        return ((int)((View)contentView.getTag()).getTag());
    }

    private OnTabSelectedListener onTabSelectedListener;
    public void setOnTabSelectedListener(OnTabSelectedListener onTabSelectedListener){
        this.onTabSelectedListener = onTabSelectedListener;
    }
    public interface OnTabSelectedListener{
        void onTabSelected(int index);
    }
}
